﻿using System;
using System.Windows.Input;
using IronCow;
using Milkify.Data;
using Milkify.Input;
using Milkify.Interfaces;

namespace Milkify.Controllers
{
    public class TaskNoteListController : Controller
    {
        private IView mView;

        public TaskNoteListController(ControllerContext context, Rtm rtm, IViewContainer viewContainer, IViewFactory viewFactory)
            : base(context, rtm, viewContainer)
        {
            mView = viewFactory.Create("TaskNoteList");
        }

        public override void Execute()
        {
            // Create commands.
            ICommand addTaskNoteCommand = new DecoupledCommand(AddTaskNote);
            ICommand navigateToTaskNoteCommand = new DecoupledCommand<TaskNote>(NavigateToTaskNote);

            // Create view data.
            var task = Context.CommandParameter as Task;
            if (task == null)
                throw new InvalidOperationException("No task was given for which to display the list of notes.");
            var viewData = new TaskNoteListViewData(task.Notes);
            viewData.AddTaskNoteCommand = addTaskNoteCommand;
            viewData.NavigateToTaskNoteCommand = navigateToTaskNoteCommand;
            mView.ViewData = viewData;

            // Change the current view.
            ViewContainer.NavigateToView(mView);
        }

        private void AddTaskNote()
        {
            var task = Context.CommandParameter as Task;
            Redirect("TaskNoteEdit", "AddTaskNote", task);
        }

        private void NavigateToTaskNote(TaskNote taskNote)
        {
            var task = Context.CommandParameter as Task;
            var parameter = new NavigateToTaskNoteParameter(task, taskNote);
            Redirect("TaskNote", "NavigateToTaskNote", parameter);
        }
    }
}
